package com.shan.domm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shan.domm.common.BaseResponse;
import com.shan.domm.common.ErrorCode;
import com.shan.domm.common.ResultUtils;
import com.shan.domm.exception.BusinessException;
import com.shan.domm.mapper.AnnouncementMapper;
import com.shan.domm.model.domain.Announcement;
import com.shan.domm.model.domain.DTOs.AnnouncementDTO;
import com.shan.domm.model.domain.Room;
import com.shan.domm.service.AnnouncementService;
import com.shan.domm.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/announcement")
@Slf4j
public class AnnouncementController {

    @Resource
    private AnnouncementService announcementService;

    @Resource
    private AnnouncementMapper announcementMapper;

    @Autowired
    private RedisUtil redisUtil;

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

    @Value("${gorit.file.root.path}")
    private String filePath;


//    @PostMapping(value="/add")
//    public BaseResponse Addanonounce(@RequestParam("file")MultipartFile file, Announcement announcement) throws IOException {
//        String format=sdf.format(new Date());;
//        String fileName=file.getOriginalFilename();
//        String newFileName=format+"_"+fileName;
//        System.out.println(announcement.getName());
//
//        File dest=new File(filePath+newFileName);
//        try{
//            file.transferTo(dest);
//            return ResultUtils.success("上传成功");
//        }catch (IOException e){
//            log.error(e.toString());
//        }
//        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"失败");
//    }

    @PostMapping(value="/addAnnouncement")
    public BaseResponse<String> Addannouncement(@RequestBody Announcement announcement){
        if(announcement==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数不能为空");
        }
        String updatedorm = announcementService.Addannouncement(announcement);
        return ResultUtils.success(updatedorm);
    }

    @PostMapping(value = "/getList")
    public BaseResponse<List<Announcement>>listPage(Integer current,Integer size){
        if(current==null||size==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        List<Announcement> announcements = new ArrayList<>();
        try {
            announcements = redisUtil.getAllList("categoryList", Announcement.class);
            if (announcements.size() != 0) {
                return ResultUtils.success(announcements);
            }
            log.warn("redis中获取不到分区数据");
        } catch (Exception e) {
            log.error("获取redis分区数据失败");
        }
        Page<Announcement> page=new Page<>(current,size);
        Page<Announcement> pageList=announcementMapper.selectPage(page,null);
        redisUtil.setAllList("categoryList",pageList.getRecords());
        return ResultUtils.success(pageList.getRecords());
    }
}
